package com.whh.permission.controller;

import com.github.pagehelper.PageInfo;
import com.whh.permission.po.Asset;
import com.whh.permission.po.Asset;
import com.whh.permission.po.Asset;
import com.whh.permission.service.IAssetService;
import com.whh.permission.util.JsonObject;
import com.whh.permission.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class AssetController {
    @Resource
    private IAssetService assetServiceImpl;

    @RequestMapping("asset/assetAll")
    @ResponseBody
    public JsonObject queryAssetInfo(Model model, Asset asset, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){
        JsonObject object=new JsonObject();
        PageInfo<Asset> pageInfo=assetServiceImpl.findAssetAll(page,limit,asset);
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }
    @RequestMapping("asset/assetByStatus")
    @ResponseBody
    public JsonObject queryAssetInfoByStatus(Model model, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit){
        Asset asset = new Asset();
        asset.setStatus(2);
        JsonObject object=new JsonObject();
        PageInfo<Asset> pageInfo=assetServiceImpl.findAssetAll(page,limit,asset);
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }

    @RequestMapping("asset/addAssetSubmit")
    @ResponseBody
    public R addAssetSubmit(Asset asset){
        asset.setCreateTime(new Date());
        assetServiceImpl.addAsset(asset);
        return R.ok();
    }

    @RequestMapping("asset/updateAssetSubmit")
    @ResponseBody
    public R updateAssetSubmit(Asset asset){
        asset.setUpdateTime(new Date());
        assetServiceImpl.updateAsset(asset);
        return R.ok();
    }

    /**
     * 删除功能
     * @return
     */
    @RequestMapping("asset/deleteAssetByIds")
    @ResponseBody
    public R deleteAssetByIds(String ids){
        List list = Arrays.asList(ids.split(","));
        assetServiceImpl.deleteAssetByIds(list);
        return R.ok();
    }

    @RequestMapping("asset")
    public String assetIndex(){
        return "pages/asset";
    }
    @RequestMapping("/addAsset")
    public String addAsset(){
        return "pages/addAsset";
    }
    @RequestMapping("/updateAsset")
    public String updateAsset(Model model,Integer id){
        Asset asset = assetServiceImpl.queryAssetById(id);
        model.addAttribute("asset",asset);
        return "pages/updateAsset";
    }
}
